package com.gaoyi.basemodule.basefragment;

import android.content.Intent;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

public abstract class BasePermissionFragment extends Fragment {

    protected ActivityResultLauncher<String[]> mPermissionsResult;

    protected abstract void onRequestPermissionsResult(Map<String, Boolean> result);

    protected abstract void onActivityResult(int requestCode, ActivityResult result);

    protected ActivityResultLauncher<Intent> registerForActivityResult(int requestCode) {
        return registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> onActivityResult(requestCode, result));
    }

    protected ActivityResultLauncher<String[]> registerForPermissionsResult() {
        return registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), this::onRequestPermissionsResult);
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }
}
